#include "game.h"
#include <stdlib.h>
#include <stdio.h>

int sclist_add(NODE** head,DATA data)
{
	NODE* pnew = (NODE*)malloc(sizeof(NODE));
	if(!pnew)
	{
		puts("创建失败!");
		return -1;
	}
	pnew -> data = data;
	pnew -> next = pnew;
	if((*head) == NULL)
	{
		*head = pnew;
		return 0;
	}

	NODE* p = *head, *q = NULL;
	while(p)
	{		
		if(p -> next == *head)
			break;
		p = p -> next;
	}
	p -> next = pnew;
	pnew -> next = *head;

}

int sclist_delete(NODE** head,int m,int* num)
{
	if(*head == NULL)
		return -1;
	NODE* p = *head, *q = NULL;
	while(p)
	{
		q = p;
		p = p -> next;
		if(p == *head)
			break;
	}
	if(m == 1)
	{
		if(p == p -> next)
		{
			*num = p -> data;
			*head = NULL;
			free(p);
			return 0;
		}
		q -> next = p -> next;
		*head = p -> next;
		free(p);
		return 0;
	}
	int n = 1;
	while(p)
	{
		q = p;
		p = p -> next;
		n++;
		if(n == m)
		{
			if(p == p -> next)
			{
				*head = NULL;
				*num = p -> data;
				free(p);
				return 0;
			}
			*head = p -> next;
			q -> next = p -> next;
			free(p);
			return 0;
		}
	}
}
/*
void sclist_free(NODE** head)
{
	
	NODE* p = *head, *q = NULL;
	while(p)
	{
		if(p -> next == *head)
			p -> next = NULL;
		q = p;
		p = p -> next;
		free(q);
	}
	*head = NULL;
	return ;
}
*/
